<?php
/**
 * 商户供应商
 * @author Administrator
 *
 */
class supplier extends CControllerMerchantBase{
	public function show()
	{
		$this->_title = "供应商列表";
		$model = new supplierModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		
		$list = $model->selectPage($pageIndex,$pageSize,"*","del_flag=0 and shop_id=".$this->_shopID,"","",$c);
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c);
		
		$this->setViewParam("userHtml","<a class='user_html' style='color:blue;' href='/merchant/supplier/add'>新增供应商</a>");
		$this->render("supplier/list",array("list"=>$list,"pageStr"=>$pageStr));
	}
	
	private function getPostInfo()
	{
		$modelInfo = array();
		$modelInfo["s_name"] = $this->getRequestData("txt_s_name");
		$modelInfo["s_phone"] = $this->getRequestData("txt_s_phone");
		$modelInfo["s_person"] = $this->getRequestData("txt_s_person");
		$modelInfo["back_name"] = $this->getRequestData("txt_back_name");
		$modelInfo["back_account"] = $this->getRequestData("txt_back_account");
		$modelInfo["remark"] = $this->getRequestData("txt_remark");
		
		return $modelInfo;
	}
	
	public function add(){
		$this->_title = "供应商新增";
		$errorStr="";
		$rule=CCustomFormValidator::supplierValidator();
		$this->formValidator->init(array("formID"=>"supplierForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = $this->getPostInfo();
			$modelInfo["shop_id"] = $this->_shopID;
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new supplierModel();
				$uid=$model->insert($modelInfo);
				
				if($uid>0)
				{
					$this->sysMsg("新增成功",0,array(array("text"=>"返回列表","href"=>"/merchant/supplier/show/"),array("text"=>"继续新增","href"=>"/merchant/supplier/add")));
				}
				else
				{
					$this->sysMsg("新增失败",1);
				}
			}
		}
		
		
		$this->render("supplier/add",array("error"=>$errorStr));
	}

	public function edit(){
		$this->_title = "供应商修改";
		$s_id = $this->getRequestData("sid","GET");
		$errorStr = "";

		$rule=CCustomFormValidator::supplierValidator();
		$this->formValidator->init(array("formID"=>"supplierForm"));
		$this->formValidator->setRules($rule);

		$model=new supplierModel();
		if($_POST){
			$modelInfo = $this->getPostInfo();
			$modelInfo["s_id"] = $s_id;
			
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->updateByPkId($modelInfo);

		        $this->redirectUrl("/merchant/supplier/show/");
			}
		}
		else{
			$modelInfo = $model->selectById($s_id,"shop_id=".$this->_shopID);
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
		}
		
		$this->render("supplier/edit",array("error"=>$errorStr,"info"=>$modelInfo));
	}
	
	public function del()
	{
		$id = $this->getRequestData("id","GET");
		if ($id)
        {
            $buc = new supplierModel();

			$buc->deleteByPkId($id);
            $this->show();
        }
	}

}